package com.okq.dao;

import com.okq.dao.base.Impl.BaseDao;
import com.okq.entity.Log;
import com.okq.util.page.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述 : 系统日志Dao
 * 作者 : zdl
 * 日期 : 2017/6/29 11:21
 */
@Repository
public class LogDao extends BaseDao {

    public void saveLog(Log log) {
        super.save(log);
    }

    public void deleteLog(Log log) {
        super.delete(log);
    }

    public void updateLog(Log log) {
        super.update(log);
    }

    public Log getLogById(String id) {
        return (Log) super.get(Log.class, id);
    }

    public List<Log> listAllLog() {
        String hql = "FROM Log WHERE isDelete = 0 ";
        return super.find(hql);
    }

    public Page<Log> pageByLog(Page<Log> page, Log log) {
        StringBuilder hqlSb = new StringBuilder();
        hqlSb.append("FROM Log l WHERE l.isDelete = 0 ");
        if (log != null) {
            Map<String, Object> param = new HashMap<>();
            String name = log.getQueryName();
            if (StringUtils.isNotBlank(name)) {
                hqlSb.append(" AND (l.userName LIKE ?1 OR l.loginName LIKE ?2) ");
                param.put("1", "%" + name + "%");
                param.put("2", "%" + name + "%");
            }
            Integer type = log.getType();
            if (type != null && type != -1) {
                hqlSb.append(" AND l.type = ?3 ");
                param.put("3", type);
            }
            hqlSb.append(" ORDER BY l.createTime DESC ");
            return super.findByPage(page, hqlSb.toString(), param);
        }
        return page;
    }

    public List<Log> listLastTenLog() {
        String hql = "FROM Log WHERE isDelete = 0 ORDER BY createTime DESC";
        return super.findMax(hql, 10);
    }
}
